package com.peenongsaban.freelance.action;

import com.opensymphony.xwork2.ActionSupport;
import com.peenongsaban.freelance.bean.RegisterBean;
import com.peenongsaban.freelance.model.JobManageModel;
import com.peenongsaban.freelance.service.JobManageService;
import com.peenongsaban.freelance.utils.ConstantUtils;
import com.peenongsaban.freelance.utils.SessionUtils;

public class JobManageAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private static String GO_TO_JOB_DETAIL = "goToJobDetail";
	private static String GO_TO_MY_JOB = "goToMyJob";

	private JobManageModel model = new JobManageModel();

	// Freelance
	public String requestJob() {
		RegisterBean bean = (RegisterBean) SessionUtils.getSession(ConstantUtils.SESSION_LOGIN);
		if(bean==null){
			bean = new RegisterBean();
		}
		JobManageService service = new JobManageService();
		service.requestJob(bean.getUser().getIdCard(), model.getPostJobId(), model.getPriceOfBid());
		return SUCCESS;
	}

	// Freelance
	public String cancelRequestJob() {
		RegisterBean bean = (RegisterBean) SessionUtils.getSession(ConstantUtils.SESSION_LOGIN);
		if(bean==null){
			bean = new RegisterBean();
		}
		JobManageService service = new JobManageService();
		service.cancelRequestJob(bean.getUser().getIdCard(), model.getPostJobId());
		if (model.getActionTo().equalsIgnoreCase("MyJob")) {
			return GO_TO_MY_JOB;
		} else{
			return GO_TO_JOB_DETAIL;
		}
	}
	
	// Freelance
	public String deleteDistApprovedRequestJob() {
		RegisterBean bean = (RegisterBean) SessionUtils.getSession(ConstantUtils.SESSION_LOGIN);
		if(bean==null){
			bean = new RegisterBean();
		}
		JobManageService service = new JobManageService();
		service.cancelRequestJob(bean.getUser().getIdCard(), model.getPostJobId());
		if (model.getActionTo().equalsIgnoreCase("MyJob")) {
			return GO_TO_MY_JOB;
		} else {
			return GO_TO_JOB_DETAIL;
		}
	}
	
	// Freelance
		public String notConfirmedRequestJob() {
			RegisterBean bean = (RegisterBean) SessionUtils.getSession(ConstantUtils.SESSION_LOGIN);
			if(bean==null){
				bean = new RegisterBean();
			}
			JobManageService service = new JobManageService();
			service.notConfirmedRequestJob(bean.getUser().getIdCard(), model.getPostJobId());
			return SUCCESS;
		}

	// Owner Job
	public String approvedRequestJob() {
		JobManageService service = new JobManageService();
		service.approvedRequestJob(model.getIdCard(), model.getPostJobId());
		return SUCCESS;
	}

	// Owner Job
	public String disApprovedRequestJob() {
		JobManageService service = new JobManageService();
		service.disApprovedRequestJob(model.getIdCard(), model.getPostJobId());
		return SUCCESS;
	}

	// Freelance
	public String confirmWoringJob() {
		RegisterBean bean = (RegisterBean) SessionUtils.getSession(ConstantUtils.SESSION_LOGIN);
		if(bean==null){
			bean = new RegisterBean();
		}
		JobManageService service = new JobManageService();
		service.confirmWorkingJob(bean.getUser().getIdCard(), model.getPostJobId());
		if (model.getActionTo().equalsIgnoreCase("MyJob")) {
			return GO_TO_MY_JOB;
		} else {
			return GO_TO_JOB_DETAIL;
		}
	}

	// Freelance
	public String estimateJob() {
		RegisterBean bean = (RegisterBean) SessionUtils.getSession(ConstantUtils.SESSION_LOGIN);
		if(bean==null){
			bean = new RegisterBean();
		}
		JobManageService service = new JobManageService();
		service.estimateJob(bean.getUser().getIdCard(), model.getPostJobId());
		return SUCCESS;
	}

	// Owner Job
	public String successJob() {
		JobManageService service = new JobManageService();
		service.successJob(model.getIdCard(), model.getPostJobId());
		return SUCCESS;
	}

	public JobManageModel getModel() {
		return model;
	}

	public void setModel(JobManageModel model) {
		this.model = model;
	}
	
	public String actionToJobDetailPage() {
		model.setUrl("jobDetail?model.postJobId="+model.getPostJobId());
		return SUCCESS;
	}

}
